package com.tungse.juc.collection;

import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.CopyOnWriteArraySet;

public class SetTest {

    public static void main(String[] args) {
//        Set<String> set = new HashSet<>();//不安全的set 集合 ConcurrentModificationException
//        Set<String> set = Collections.synchronizedSet(new HashSet<>());//线程安全的set 集合
        Set<String> set = new CopyOnWriteArraySet<>();//线程安全的set 集合
        for (int i = 0; i < 100; i++) {
            new Thread(()->{
                set.add(UUID.randomUUID().toString().substring(1,5));
                System.out.println(set);
            }, i+"").start();
        }
    }
}
